package com.shujia.flink.tf

import org.apache.flink.streaming.api.scala.function.AllWindowFunction
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow
import org.apache.flink.util.Collector

object Demo8WindowAll {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    val kvDS: DataStream[(String, Int)] = linesDS
      .flatMap(_.split(","))
      .map((_, 1))

    /**
     * windowAll: 将所有的数据放到同一个窗口中
     */
    val windowDS: AllWindowedStream[(String, Int), TimeWindow] = kvDS
      .windowAll(TumblingProcessingTimeWindows.of(Time.seconds(5)))

    val countDS: DataStream[(String, Int)] = windowDS.sum(1)

    //countDS.print()

    /**
     * 在windowAll这个之后使用apply
     */

    val applyDS: DataStream[(String, Int)] = windowDS.apply(new AllWindowFunction[(String, Int), (String, Int), TimeWindow] {

      /**
       * apply每一个窗口执行一次
       *
       * @param window ： 窗口对象，可以获取窗口的开始和结束时间
       * @param input  ： 一个窗口内所有的数据
       * @param out    ： 用于将结果发送到下游
       */
      override def apply(window: TimeWindow,
                         input: Iterable[(String, Int)],
                         out: Collector[(String, Int)]): Unit = {
        //一个窗口内所有的单词，可以写代码处理一个窗口内所有的数据
        val list: List[(String, Int)] = input.toList

        //统计单词的数量
        val countList: Map[String, Int] = list
          .groupBy(_._1)
          .map(kv => (kv._1, kv._2.length))

        //将结果发送到下游
        for ((word, count) <- countList) {
          //将数据发送到下游
          out.collect((word, count))
        }
      }
    })

    applyDS.print()

    env.execute()
  }

}
